package com.whnk.pm.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.whnk.common.pm.enums.PmDoStatus;
import com.whnk.common.support.Convert;
import com.whnk.pm.domain.SusdInfo;
import com.whnk.pm.mapper.SusdDetailMapper;
import com.whnk.pm.mapper.SusdInfoMapper;
import com.whnk.pm.service.ISusdInfoService;

/**
 * 补助基本 服务层实现
 * 
 * @author whwhnk
 * @date 2019-07-27
 */
@Service
public class SusdInfoServiceImpl implements ISusdInfoService {
	@Autowired
	private SusdInfoMapper susdInfoMapper;

	@Autowired
	private SusdDetailMapper susdDetailMapper;

	/**
	 * 查询补助基本信息
	 * 
	 * @param susdId
	 *            补助基本ID
	 * @return 补助基本信息
	 */
	@Override
	public SusdInfo selectSusdInfoById(Integer susdId) {
		return susdInfoMapper.selectSusdInfoById(susdId);
	}

	/**
	 * 查询补助基本列表
	 * 
	 * @param susdInfo
	 *            补助基本信息
	 * @return 补助基本集合
	 */
	@Override
	public List<SusdInfo> selectSusdInfoList(SusdInfo susdInfo) {
		return susdInfoMapper.selectSusdInfoList(susdInfo);
	}

	/**
	 * 新增补助基本
	 * 
	 * @param susdInfo
	 *            补助基本信息
	 * @return 结果
	 */
	@Override
	public int insertSusdInfo(SusdInfo susdInfo) {
		return susdInfoMapper.insertSusdInfo(susdInfo);
	}

	/**
	 * 修改补助基本
	 * 
	 * @param susdInfo
	 *            补助基本信息
	 * @return 结果
	 */
	@Override
	public int updateSusdInfo(SusdInfo susdInfo) {
		return susdInfoMapper.updateSusdInfo(susdInfo);
	}

	/**
	 * 删除补助基本对象
	 * 
	 * @param ids
	 *            需要删除的数据ID
	 * @return 结果
	 */
	@Override
	public int deleteSusdInfoByIds(String ids) {
		String[] idsArr = Convert.toStrArray(ids);

		StringBuilder idsStr = new StringBuilder("");
		for (String id : idsArr) {
			idsStr.append(id).append(";");
		}

		// 删除补助明细
		susdDetailMapper.delSusdDetailBySusdIds(idsArr);

		return susdInfoMapper.deleteSusdInfoByIds(Convert.toStrArray(ids));
	}

	/**
	 * TODO 生成补助明细信息
	 * 
	 * @param listId
	 */
	@Override
	public void genSusdDetail(List<String> listId) {
		/**
		 *  根据考勤导入的信息，统计本地补助。
		 *  
		 	INSERT INTO pm_susd_detail(susd_id, emp_id, emp_nm, wow_type, sude_type, sude_days, sude_amount,sign_days, sude_note)
			SELECT 
				#{susdId}, t1.emp_id, t1.emp_nm, t1.wow_type, t1.sude_type, SUM(t1.sign_pd) sude_days, 
				CASE
					when  t1.wow_type='0' OR t1.wow_type='2' then 0
					when  t1.wow_type='3' OR (t1.sude_type='0' OR t1.sude_type='1' OR t1.sude_type='2') then SUM(t1.sign_pd*s.sur_price)
					when (t1.wow_type='1') and (t1.sude_type='3' OR t1.sude_type='4' OR t1.sude_type='5') and t1.sign_hours>=8 then SUM(8*s.sur_price)
					when (t1.wow_type='1') and (t1.sude_type='3' OR t1.sude_type='4' OR t1.sude_type='5') and (t1.sign_hours>=4 and t1.sign_hours<8) then SUM(4*s.sur_price)
				END sude_amount,
				SUM(t1.sign_pd) sign_days,
				'根据客户考勤信息和节假日加班信息计算的补助' sude_note
			FROM 
			(
				SELECT 
					t.emp_id,t.emp_nm,w.wow_type,t.sign_dt,t.sign_pd,t.sign_hours,d.wd_dt_type,m.wl_level,
				CASE
					when d.wd_dt_type='0' and SUBTIME(TIME(t.sign_edti),TIME('21:00:00'))<0  then '0' 
					when d.wd_dt_type='0' and SUBTIME(TIME(t.sign_edti),TIME('21:00:00'))>=0 then '1' 
	        		when d.wd_dt_type='4' and SUBTIME(TIME(t.sign_edti),TIME('21:00:00'))<0  then '0' 
					when d.wd_dt_type='4' and SUBTIME(TIME(t.sign_edti),TIME('21:00:00'))>=0 then '1' 
					when d.wd_dt_type='1' then '3' 
					when d.wd_dt_type='3' then '4' 
	        		when d.wd_dt_type='2' then '5' 
				END sude_type,
				CASE 
					when (d.wd_dt_type=0 OR d.wd_dt_type=4 OR w.wow_type=3) then -1
		        ELSE 
		            m.wl_level
				END wl_level_1
				FROM pm_imp_sign_info t 
				LEFT JOIN pm_pro_info p ON t.pro_sn = p.pro_sn
				LEFT JOIN pm_wd_info d ON t.sign_dt = d.wd_date
				LEFT JOIN pm_pro_mem m ON (t.emp_id=m.emp_id and p.pro_id=m.pro_id)
	            LEFT JOIN pm_wow_info w ON (t.emp_id=w.emp_id AND t.sign_dt=w.wow_dt)
				WHERE t.sign_pd>=1 and p.pro_id=#{proId} AND (t.sign_dt BETWEEN #{susdStdt} AND #{susdEddt} )
			) t1
			LEFT JOIN pm_susd_rule s ON (t1.wd_dt_type=s.wd_dt_type AND t1.sude_type=s.sude_type AND t1.wl_level_1=IFNULL(s.wl_level,-1))
			GROUP BY t1.emp_id,t1.emp_nm, t1.wow_type, t1.sude_type
		 */
		
		/**
		 * 根据出差信息，统计出差补助。
		 * 
		 * INSERT INTO pm_susd_detail(susd_id, emp_id, emp_nm, sude_type, sude_days, sude_amount,sign_days, sude_note)
			SELECT DISTINCT #{susdId},t.emp_id,t.emp_nm,'2' sude_type,
			SUM(CASE
				WHEN ( DATEDIFF(b.busi_stdt,#{susdStdt})<=0 AND DATEDIFF(b.busi_eddt,#{susdEddt})<=0 ) THEN (DATEDIFF(b.busi_eddt,#{susdStdt})+1)
				WHEN ( DATEDIFF(b.busi_stdt,#{susdStdt})>=0 AND DATEDIFF(b.busi_eddt,#{susdEddt})<=0 ) THEN (DATEDIFF(b.busi_eddt,b.busi_stdt)+1)
				WHEN ( DATEDIFF(b.busi_stdt,#{susdStdt})<=0 AND DATEDIFF(b.busi_eddt,#{susdEddt})>=0 ) THEN (DATEDIFF(#{susdEddt},#{susdStdt})+1)
				WHEN ( DATEDIFF(b.busi_stdt,#{susdStdt})>=0 AND DATEDIFF(b.busi_eddt,#{susdEddt})>=0 ) THEN (DATEDIFF(#{susdEddt},b.busi_stdt)+1)
			END) sude_days,
			SUM(CASE
				WHEN ( DATEDIFF(b.busi_stdt,#{susdStdt})<=0 AND DATEDIFF(b.busi_eddt,#{susdEddt})<=0 ) THEN (DATEDIFF(b.busi_eddt,#{susdStdt})+1)*70
				WHEN ( DATEDIFF(b.busi_stdt,#{susdStdt})>=0 AND DATEDIFF(b.busi_eddt,#{susdEddt})<=0 ) THEN (DATEDIFF(b.busi_eddt,b.busi_stdt)+1)*70
				WHEN ( DATEDIFF(b.busi_stdt,#{susdStdt})<=0 AND DATEDIFF(b.busi_eddt,#{susdEddt})>=0 ) THEN (DATEDIFF(#{susdEddt},#{susdStdt})+1)*70
				WHEN ( DATEDIFF(b.busi_stdt,#{susdStdt})>=0 AND DATEDIFF(b.busi_eddt,#{susdEddt})>=0 ) THEN (DATEDIFF(#{susdEddt},b.busi_stdt)+1)*70
			END) sude_amount,
			SUM(CASE
				WHEN ( DATEDIFF(b.busi_stdt,#{susdStdt})<=0 AND DATEDIFF(b.busi_eddt,#{susdEddt})<=0 ) THEN (DATEDIFF(b.busi_eddt,#{susdStdt})+1)
				WHEN ( DATEDIFF(b.busi_stdt,#{susdStdt})>=0 AND DATEDIFF(b.busi_eddt,#{susdEddt})<=0 ) THEN (DATEDIFF(b.busi_eddt,b.busi_stdt)+1)
				WHEN ( DATEDIFF(b.busi_stdt,#{susdStdt})<=0 AND DATEDIFF(b.busi_eddt,#{susdEddt})>=0 ) THEN (DATEDIFF(#{susdEddt},#{susdStdt})+1)
				WHEN ( DATEDIFF(b.busi_stdt,#{susdStdt})>=0 AND DATEDIFF(b.busi_eddt,#{susdEddt})>=0 ) THEN (DATEDIFF(#{susdEddt},b.busi_stdt)+1)
			END) sign_days,
			'根据出差信息计算的补助' sude_note
			FROM pm_pro_mem t 
			LEFT JOIN pm_pro_info p ON t.pro_id = p.pro_id
			LEFT JOIN pm_busitrip_info b ON t.emp_id=b.emp_id
			WHERE p.pro_id=2 and b.busi_stdt is not null and b.busi_eddt is not null
			GROUP BY t.emp_id,t.emp_nm
		 */
		
		if(null!= listId && listId.size()>0) {
			
			SusdInfo susdInfo = null;
			
			for(String susdId : listId) {
				
				// 根据补助基本信息ID，查询补助基本信息
				susdInfo = susdInfoMapper.selectSusdInfoById(Integer.valueOf(susdId));
				
				if(null != susdInfo) {
					try {
						
						// 根据补助基本信息ID，删除已有的补助明细
						susdDetailMapper.delSusdDetailBySusdIds(Convert.toStrArray(susdId));
						
						// 生成本地补助明细
						susdInfoMapper.addSusdDetailsBySign(susdInfo);
						
						// 生成出差补助明细
						susdInfoMapper.addSusdDetailsByBt(susdInfo);
						
						// 修改为成功状态
						susdInfo.setSusdStat(PmDoStatus.SUCCESS.getCode());
						
					}catch(Exception e){
						
						// 修改为失败状态
						susdInfo.setSusdStat(PmDoStatus.FAILURE.getCode());
						
					}finally {
						
						// 修改补助基本信息表的执行状态
						susdInfoMapper.updateSusdInfo(susdInfo);
						
					}
				}
			}
		}
	}
}
